package com.autonavi.yunda.yunji.controller.request;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.core.enums.EngineType;
import com.autonavi.yunda.yunji.core.enums.InterfaceStatus;
import com.autonavi.yunda.yunji.core.model.dto.TaskConfigDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;

/**
 * @author cizhong.wcs
 * @date 2023/12/19 17:02
 */
@Data
public class TaskConfigRequest {
    private Long id;
    private String name;
    private String projectId;
    private String description;
    private String engineData;
    private String extraData;
    private InterfaceStatus status;
    private EngineType engineType;

    public TaskConfigDTO toAddDTO(String operator) {
        AmapPreconditions.checkArgumentForUser(projectId != null, "projectId不能为空");
        return TaskConfigDTO.builder()
                .projectId(projectId)
                .name(name)
                .description(description)
                .engineData(engineData)
                .extraData(extraData)
                .description(description)
                .status(InterfaceStatus.INIT)
                .createUser(operator)
                .updateUser(operator)
                .engineType(engineType)
                .build();

    }

    public UpdateWrapper<TaskConfigDTO> toUpdateWrapper(String operator) {
        AmapPreconditions.checkArgumentForUser(id != null, "id不能为空");
        QueryWrapper<TaskConfigDTO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        UpdateWrapper<TaskConfigDTO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        if (Strings.isNotBlank(name)) {
            updateWrapper.set("name", name);
        }
        if (Strings.isNotBlank(engineData)) {
            updateWrapper.set("engine_data", engineData);
        }
        if (Strings.isNotBlank(extraData)) {
            updateWrapper.set("extra_data", extraData);
        }
        if (Strings.isNotBlank(description)) {
            updateWrapper.set("description", description);
        }
        if (Strings.isNotBlank(operator)) {
            updateWrapper.set("update_user", operator);
        }
        return updateWrapper;
    }

    public TaskConfigDTO toValidatingDTO(String empId) {
        TaskConfigDTO dto = toAddDTO(empId);
        dto.setStatus(InterfaceStatus.VALIDATING);
        return dto;
    }
}
